
This notebook illustrates the use of GPS traces shared publicly by OSM community members in GPX format.
import numpy as np
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas
import matplotlib.pyplot as plt
from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts, dim
from os.path import exists
from urllib.request import urlretrieve
import warnings
warnings.filterwarnings('ignore')
plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True}
opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))
hvplot_defaults = {'tiles':None, 'cmap':'Viridis', 'colorbar':True}
mpd.show_versions()
MovingPandas 0.15.rc1 SYSTEM INFO ----------- python : 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:39:05) [MSC v.1929 64 bit (AMD64)] executable : H:\miniconda3\envs\mpd-ex\python.exe machine : Windows-10-10.0.19045-SP0 GEOS, GDAL, PROJ INFO --------------------- GEOS : None GEOS lib : None GDAL : 3.5.0 GDAL data dir: None PROJ : 9.0.0 PROJ data dir: H:\miniconda3\pkgs\proj-9.0.0-h1cfcee9_1\Library\share\proj PYTHON DEPENDENCIES ------------------- geopandas : 0.12.2 pandas : 1.5.3 fiona : 1.8.21 numpy : 1.24.1 shapely : 1.8.2 rtree : 1.0.0 pyproj : 3.3.1 matplotlib : 3.6.3 mapclassify: None geopy : 2.3.0 holoviews : 1.14.9 hvplot : 0.8.2 geoviews : 1.9.6 stonesoup : 0.1b11
def get_osm_traces(page=0, bbox='16.18,48.09,16.61,48.32'):
file = 'osm_traces.gpx'
url = f'https://api.openstreetmap.org/api/0.6/trackpoints?bbox={bbox}&page={page}'
if not exists(file):
urlretrieve(url, file)
gdf = gpd.read_file(file, layer='track_points')
# OPTIONAL: dropping empty columns
gdf.drop(columns=['ele', 'course', 'speed', 'magvar', 'geoidheight', 'name', 'cmt', 'desc',
'src', 'url', 'urlname', 'sym', 'type', 'fix', 'sat', 'hdop', 'vdop',
'pdop', 'ageofdgpsdata', 'dgpsid'], inplace=True)
return gdf
gdf = get_osm_traces()
osm_traces = mpd.TrajectoryCollection(gdf, 'track_fid', t='time')
print(f'The OSM traces download contains {len(osm_traces)} tracks')
The OSM traces download contains 1 tracks
for track in osm_traces: print(f'Track {track.id}: length={track.get_length():.0f}m')
Track 0: length=3964m
track.plot()
<AxesSubplot: >
Generalization is optional but speeds up rendering
osm_traces = mpd.MinTimeDeltaGeneralizer(osm_traces).generalize(tolerance=timedelta(minutes=1))
osm_traces.hvplot(title='OSM Traces', line_width=7, width=700, height=400)
osm_traces.get_trajectory(0).hvplot(title='Speed (m/s) along track', c='speed', cmap='RdYlBu',
line_width=7, width=700, height=400, tiles='CartoLight', colorbar=True)